package com.link510.aitools.services;

import com.google.common.collect.Lists;
import com.link510.aitools.core.data.model.DeviceListModel;
import com.link510.aitools.core.data.model.DeviceShowModel;
import com.link510.aitools.core.domain.GpioInfo;
import com.link510.aitools.core.errors.SateCollect;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.data.DeviceData;
import com.orhanobut.logger.Logger;

import java.util.List;

import io.reactivex.schedulers.Schedulers;
import lombok.Getter;
import lombok.Setter;

public class Devices extends BaseService {

    private static Devices mInstance;

    public static Devices getInstance() {

        if (mInstance == null) {

            synchronized (Devices.class) {

                if (mInstance != null) {
                    return mInstance;
                }

                mInstance = new Devices();

            }
        }

        return mInstance;
    }

    @Getter
    private boolean openDoorStatus = false;


    @Getter
    @Setter
    private List<GpioInfo> gpioInfoList = Lists.newArrayList();


    /**
     * 获取最新版本信息
     *
     * @param listener 版本信息
     */
    public void get(String deviceSN, CWMListener<DeviceShowModel> listener) {
        try {

            DeviceData.getInstance().get(deviceSN)
                    .subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {
                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        DeviceShowModel model = messageInfo.getContent();

                        if (model == null) {
                            onErrorAction(listener, -4, "数据解析异常");
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), model);
                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "获取数据发生异常");
                            Logger.e(t, "获取数据发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });
        } catch (Exception ex) {
            Logger.e(ex, "获取版本数据发生异常");
            onErrorAction(listener, -1, "获取版本数据发生异常");
        }
    }


    /**
     * 获取广告列表
     *
     * @return 返回广告列表
     */
    public void list(String deviceSN, CWMListener<DeviceListModel> listener) {
        try {

            DeviceData.getInstance().list(deviceSN)
                    .subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {
                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        DeviceListModel model = messageInfo.getContent();

                        if (model == null) {
                            onErrorAction(listener, -4, "数据解析异常");
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), model);
                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "获取数据发生异常");
                            Logger.e(t, "获取数据发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });
        } catch (Exception ex) {
            Logger.e(ex, "获取版本数据发生异常");
            onErrorAction(listener, -1, "获取版本数据发生异常");
        }
    }

}
